/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entitydao;

import database.Database;
import entity.Navstevnik;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lattis
 */
public class NavstevnikDAO extends TableDAO implements DAO<Navstevnik>{

    private static final String TABLE_NAME = "LIB_NAVSTEVNIK";
    private static final String FIND_ALL = "SELECT * FROM LIB_NAVSTEVNIK ORDER BY ID_NAVSTEVNIK ASC";
    private static final String FIND_BY_ID = "SELECT * FROM LIB_NAVSTEVNIK WHERE ID_NAVSTEVNIK = ?";
    private static final String FIND_BY_MAIL = "SELECT * FROM LIB_NAVSTEVNIK WHERE MAIL = ?";
    private static final String FIND_BY_LOGIN = "SELECT * FROM LIB_NAVSTEVNIK WHERE LOGIN = ?";
    private static final String FIND_BY_PRIJMENI = "SELECT * FROM LIB_NAVSTEVNIK WHERE PRIJMENI = ?";
    private static final String UPDATE_MAIL = "UPDATE LIB_NAVSTEVNIK SET MAIL= ? WHERE LOGIN= ?";
    private static final String UPDATE_PASS = "UPDATE LIB_NAVSTEVNIK SET PASS= ? WHERE LOGIN= ?";
    private static final String DELETE = "DELETE FROM LIB_NAVSTEVNIK WHERE LOGIN = ?";
    private static final String INSERT = "INSERT INTO LIB_NAVSTEVNIK VALUES(?,?,?,?,?,?)";

    private Connection mConnection;

    public NavstevnikDAO(Database database) {
        super(database, TABLE_NAME);
        mConnection = database.Connection();
    }

    public List<Navstevnik> findAll() {
        List<Navstevnik> navstevnikList = new ArrayList<Navstevnik>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            pstm = mConnection.prepareStatement(FIND_ALL);
            rs = pstm.executeQuery();
            while(rs.next()){
                navstevnikList.add(new Navstevnik(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6)
                        ));
            }
        }catch(SQLException e){
            System.err.println(e.getMessage());
            navstevnikList = null;
        }finally{
            close(rs, pstm);
        }
        return navstevnikList;
    }

    public Navstevnik findById(int id) {
        Navstevnik navstevnik = null ;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            pstm = mConnection.prepareStatement(FIND_BY_ID);
            pstm.setInt(1, id);
            rs = pstm.executeQuery();
            while(rs.next()){
                navstevnik = new Navstevnik(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6)
                        );
            }
        }catch(SQLException e){
            System.err.println(e.getMessage());
            navstevnik = null;
        }finally{
            close(rs, pstm);
        }
        return navstevnik;
    }

    public Navstevnik findByMail(String mail){
        Navstevnik navstevnik = null ;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            pstm = mConnection.prepareStatement(FIND_BY_MAIL);
            pstm.setString(1, mail);
            rs = pstm.executeQuery();
            while(rs.next()){
                navstevnik = new Navstevnik(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6)
                        );
            }
        }catch(SQLException e){
            System.err.println(e.getMessage());
            navstevnik = null;
        }finally{
            close(rs, pstm);
        }
        return navstevnik;
    }

    public Navstevnik findByLogin(String login){
        Navstevnik navstevnik = null ;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            pstm = mConnection.prepareStatement(FIND_BY_LOGIN);
            pstm.setString(1, login);
            rs = pstm.executeQuery();
            while(rs.next()){
                navstevnik = new Navstevnik(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6)
                        );
            }
        }catch(SQLException e){
            System.err.println(e.getMessage());
            navstevnik = null;
        }finally{
            close(rs, pstm);
        }
        return navstevnik;
    }

    public List<Navstevnik> findByPrijmeni(String prijmeni){
        List<Navstevnik> navstevnikList = new ArrayList<Navstevnik>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            pstm = mConnection.prepareStatement(FIND_BY_PRIJMENI);
            pstm.setString(1, prijmeni);
            rs = pstm.executeQuery();
            while(rs.next()){
                navstevnikList.add(new Navstevnik(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6)
                        ));
            }
        }catch(SQLException e){
            System.err.println(e.getMessage());
            navstevnikList = null;
        }finally{
            close(rs, pstm);
        }
        return navstevnikList;
    }

    public boolean insert(Navstevnik entity) {
        PreparedStatement pstm = null;
        boolean ret = false;
        int rownum = 0;
        try {
            pstm = mConnection.prepareStatement(INSERT);
            pstm.setInt(1, entity.getId_navstevnik());
            pstm.setString(2, entity.getJmeno());
            pstm.setString(3, entity.getPrijmeni());
            pstm.setString(4, entity.getLogin());
            pstm.setString(5, entity.getPass());
            pstm.setString(6, entity.getMail());
            mDatabase.BeginTransaction();
            mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
//            String s = "insert into lib_navstevnik(jmeno,prijmeni,mail,login,pass) values('"+ entity.getJmeno()+"','"
//                    + entity.getPrijmeni()+"','"
//                    + entity.getMail()+"','"
//                    + entity.getLogin()+"','"
//                    + entity.getPass()+"')";
            pstm.executeUpdate();
//            System.err.println("ok");
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
//            System.err.println("Navstevnik ROLLBACK > " + e.getMessage());
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
            return ret;
        }
    }

    public boolean delete(Navstevnik entity) {
        PreparedStatement pstm = null;
        boolean ret = false;
        int rownum = 0;
        try {
            pstm = mConnection.prepareStatement(DELETE);
            pstm.setString(1, entity.getLogin());
            mDatabase.BeginTransaction();
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
//            System.err.println("Navstevnik delete ROLLBACK > "+ e.getMessage());
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
        }
        return false;
    }

    public boolean update(Navstevnik entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean updateMail(String mail, String login){
        PreparedStatement pstm = null;
        boolean ret = false;
        int rownum = 0;
        try {
            pstm = mConnection.prepareStatement(UPDATE_MAIL);
            pstm.setString(1, mail);
            pstm.setString(2, login);
            mDatabase.BeginTransaction();
            mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
            System.err.println("Navstevnik updateMail ROLLBACK > "+ e.getMessage());
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
        }
        return false;
    }

    public boolean updatePass(String pass, String login){
        PreparedStatement pstm = null;
        boolean ret = false;
        int rownum = 0;
        try {
            pstm = mConnection.prepareStatement(UPDATE_PASS);
            pstm.setString(1, pass);
            pstm.setString(2, login);
            mDatabase.BeginTransaction();
            mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
            System.err.println("Navstevnik updatePass ROLLBACK > "+ e.getMessage());
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
        }
        return false;
    }

    public List<Navstevnik> read(ResultSet rs) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void close(ResultSet rs, PreparedStatement pstm) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstm != null) {
                pstm.close();
            }
        } catch (Exception e) {
            System.err.println("Closing Error");
        }
    }

}
